package com.aiyige.page.my.download.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.db.table.DownloadFile;
import com.aiyige.page.my.download.util.DownloadUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.LabelProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.vondear.rxtools.RxDataTool;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/11/30.
 */

public class DownloadFileAdapter extends BaseQuickAdapter<DownloadFile, DownloadFileAdapter.ViewHolder> {
    public DownloadFileAdapter() {
        super(R.layout.download_file_item, new LinkedList<DownloadFile>());
    }

    @Override
    protected void convert(ViewHolder helper, DownloadFile item) {
        helper.bindData(item);
    }


    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.selectIv)
        ImageView selectIv;
        @BindView(R.id.selectLayout)
        FrameLayout selectLayout;
        @BindView(R.id.orderTv)
        TextView orderTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.statusIv)
        ImageView statusIv;
        @BindView(R.id.statusLayout)
        ViewGroup statusLayout;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.labelProgressBar)
        LabelProgressBar labelProgressBar;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.downloadedSizeTv)
        TextView downloadedSizeTv;
        @BindView(R.id.totalSizeTv)
        TextView totalSizeTv;
        @BindView(R.id.downloadSizeLayout)
        LinearLayout downloadSizeLayout;
        @BindView(R.id.durationTv)
        TextView durationTv;
        DownloadFile data;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.statusLayout);
        }

        public void bindData(DownloadFile item) {
            data = item;
            selectLayout.setVisibility(data.isEdit() ? View.VISIBLE : View.GONE);
            selectIv.setVisibility(data.isSelected() ? View.VISIBLE : View.INVISIBLE);
            titleTv.setText(data.getTitle());
            orderTv.setText(String.valueOf(getAdapterPosition() + 1));
            Glide.with(itemView.getContext())
                    .load(data.getCoverLocalUrl())
                    .error(Glide.with(itemView.getContext())
                            .load(data.getCoverRemoteUrl())
                            .apply(RequestOptions.placeholderOf(android.R.color.white)))
                    .into(coverIv);
            switch (data.getProgressStatus()) {
                case DownloadUtil.PROGRESS_STATUS_WAITING_DOWNLOAD_COVER:
                case DownloadUtil.PROGRESS_STATUS_DOWNLOADING_COVER:
                case DownloadUtil.PROGRESS_STATUS_DOWNLOAD_COVER_FINISHED:
                case DownloadUtil.PROGRESS_STATUS_DOWNLOAD_COVER_FAILED:
                case DownloadUtil.PROGRESS_STATUS_WAITING_DOWNLOAD:
                case DownloadUtil.PROGRESS_STATUS_DOWNLOADING:
                case DownloadUtil.PROGRESS_STATUS_DOWNLOAD_FAILED:
                    labelProgressBar.setVisibility(View.VISIBLE);
                    if (data.getTotalSize() != 0) {
                        labelProgressBar.setProgress(100f * data.getDownloadedSize() / data.getTotalSize());
                    } else {
                        labelProgressBar.setProgress(0);
                    }
                    durationTv.setVisibility(View.INVISIBLE);
                    downloadSizeLayout.setVisibility(View.VISIBLE);
                    statusLayout.setVisibility(View.VISIBLE);
                    totalSizeTv.setText(RxDataTool.byte2FitSize(data.getTotalSize()));
                    downloadedSizeTv.setText(RxDataTool.byte2FitSize(data.getDownloadedSize()));
                    if (data.getControlOption() == DownloadUtil.CONTROL_OPTION_PAUSE) {
                        statusIv.setImageResource(R.drawable.suspend);
                    } else {
                        statusIv.setImageResource(R.drawable.download);
                    }
                    break;
                case DownloadUtil.PROGRESS_STATUS_DOWNLOAD_FINISHED:
                    labelProgressBar.setVisibility(View.INVISIBLE);
                    statusLayout.setVisibility(View.INVISIBLE);
                    durationTv.setVisibility(View.VISIBLE);
                    durationTv.setText(StringUtils.formatMediaDurationMS(data.getDuration()));
                    downloadSizeLayout.setVisibility(View.INVISIBLE);
                    break;
            }


        }
    }
}
